package com.epfresh.supplier.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.epfresh.api.entity.RequestEntityMapSupply;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.global.BaseActivity;
import com.epfresh.api.global.BaseApplication;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.api.utils.JsonUtils;
import com.epfresh.supplier.R;
import com.epfresh.supplier.adapter.ClassifyViewBinder;
import com.epfresh.supplier.entity.ContentEntity;
import com.epfresh.supplier.entity.ProductClassify;
import com.epfresh.supplier.entity.StoreClassify;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import multitype.MultiTypeAdapter;
import multitype.OnRecyclerViewItemClickListener;

public class ProductClassifyPlatActivity extends BaseActivity {


    RecyclerView rv;
    MultiTypeAdapter mMultiAdapter;
    ClassifyViewBinder classifyViewBinder;
    List<ProductClassify> mItems = new ArrayList<>();

    //1,选择分类
    int type = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (savedInstanceState != null) {
            getIntent().putExtra("type", savedInstanceState.getInt("type"));
            getIntent().putExtra("categoryName", savedInstanceState.getString("categoryName"));
            getIntent().putExtra("categoryId", savedInstanceState.getString("categoryId"));
        }
        setContentView(R.layout.activity_classify_plat);

        type = getIntent().getIntExtra("type", -1);
        toolbarTitle.setText("选择系统分类");
        ivBack.setVisibility(View.VISIBLE);
        tvBack.setVisibility(View.VISIBLE);

      /*  rightbtn.setText("编辑");
        rightbtn.setTextColor(0xff6f6f6f);
        rightbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edit();
            }
        });*/

        rv = (RecyclerView) findViewById(R.id.rv);

        if (type == 1) {
            rightbtn.setVisibility(View.GONE);
        } else {
            rightbtn.setVisibility(View.VISIBLE);
        }
        LinearLayoutManager lm = new LinearLayoutManager(this);
        rv.setLayoutManager(lm);
        classifyViewBinder = new ClassifyViewBinder();

        classifyViewBinder.setTopCategory(true);
        classifyViewBinder.setOnRecyclerViewItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position, Object data) {
                if (position < mItems.size() && data != null && data instanceof ProductClassify) {
                    onItem(position, (ProductClassify) data);
                }
            }
        });
        mMultiAdapter = new MultiTypeAdapter();

        mMultiAdapter.register(ProductClassify.class, classifyViewBinder);

        rv.setAdapter(mMultiAdapter);

        mMultiAdapter.setItems(mItems);

        reqTopStoreCategory();


    }

    @Override
    protected void onRestart() {
        reqTopStoreCategory();
        super.onRestart();
    }

    private void onItem(int position, ProductClassify data) {

        Intent intent = new Intent(this, ProductClassifyManagerActivity.class);
//        intent.putExtra("categoryName", data.getName());
        intent.putExtra("topCategoryId", data.getId());


        intent.putExtra("type", 2);
        startActivity(intent);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putInt("type", type);
        outState.putString("categoryName", getIntent().getStringExtra("categoryName"));
        outState.putString("categoryId", getIntent().getStringExtra("categoryId"));
        super.onSaveInstanceState(outState);

    }


    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.toolbar_title:
            case R.id.toolbar_title_flag:


                break;


            case R.id.tv_next:


                break;
        }
    }


    private void edit() {


    }


    private void reqTopStoreCategory() {


        RequestEntityMapSupply RequestEntityMapSupply = new RequestEntityMapSupply();
        RequestEntityMapSupply.setCmd(com.epfresh.api.constant.AppSupplyRouterConstant.cmd_store_storeTopCategory_topCategoryOrCategoryList);

       /* "storeId": "",
        "pageNumber": "1",
        "pageSize": "10"*/
        RequestEntityMapSupply.putParameter("storeId", BaseApplication.getInstance().getStoreId());
        RequestEntityMapSupply.putParameter("merchantId", BaseApplication.getInstance().getMerchantId());

        request(RequestEntityMapSupply, com.epfresh.api.constant.AppSupplyRouterConstant.cmd_store_storeTopCategory_topCategoryOrCategoryList, onTopStoreCategoryRequestListener);
    }

    private OnRequestListener<StoreClassify> onTopStoreCategoryRequestListener = new OnRequestListener<StoreClassify>() {
        @Override
        public StoreClassify jsonToObj(String responseStr) {
            return new Gson().fromJson(responseStr, StoreClassify.class);
        }

        @Override
        public void onFail(int failCode, Object arg, Object tag) {

        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {

        }

        @Override
        public void onStart(Object tag) {

        }

        @Override
        public void onResponse(ResponseEntity<StoreClassify> response, Object tag) {

            if (response != null) {
                StoreClassify result = response.getResponseElement();
/*                if (responseElement != null) {
                    StoreClassify result = responseElement.getResult();*/
                    if (result != null) {
                        List<ProductClassify> topCategories = result.getTopCategories();
                        if (topCategories != null && topCategories.size() > 0) {
                            mItems.clear();
                            mItems.addAll(topCategories);
                            mMultiAdapter.notifyDataSetChanged();
                        }
                    }
                }
            }

        //}
    };


}
